from rest_framework import serializers
from system.models import Role, Menu
from django.utils import timezone


class RolePageSerializer(serializers.ModelSerializer):
    """角色分页列表序列化器"""
    createTime = serializers.DateTimeField(source='create_time', format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = Role
        fields = ['id', 'name', 'code', 'sort', 'status', 'data_scope', 'createTime']


class RoleFormSerializer(serializers.ModelSerializer):
    """角色表单信息序列化器"""
    dataScope = serializers.IntegerField(source='data_scope', required=False)

    class Meta:
        model = Role
        fields = ['id', 'name', 'code', 'sort', 'status', 'dataScope']

    def update(self, instance, validated_data):
        """更新角色信息"""
        # 设置更新时间
        validated_data['update_time'] = timezone.now()

        # 更新角色信息
        for attr, value in validated_data.items():
            setattr(instance, attr, value)

        instance.save()
        return instance


class RoleCreateSerializer(serializers.ModelSerializer):
    """角色创建序列化器"""
    dataScope = serializers.IntegerField(source='data_scope', required=False)

    class Meta:
        model = Role
        fields = ['id', 'name', 'code', 'sort', 'status', 'dataScope']
        extra_kwargs = {
            'name': {'required': True},
            'code': {'required': True},
            'sort': {'required': False},
            'status': {'required': False, 'default': 1},
        }

    @staticmethod
    def validate_name(value):
        """验证角色名称唯一性"""
        # 检查是否存在同名角色(未删除的)
        if Role.objects.filter(name=value, is_deleted=0).exists():
            raise serializers.ValidationError("角色名称已存在")
        return value

    @staticmethod
    def validate_code(value):
        """验证角色编码唯一性"""
        # 检查是否存在同编码角色(未删除的)
        if Role.objects.filter(code=value, is_deleted=0).exists():
            raise serializers.ValidationError("角色编码已存在")
        return value

    def create(self, validated_data):
        # 从请求中获取创建者ID
        request = self.context.get('request')
        if request and hasattr(request, 'user'):
            validated_data['create_by'] = request.user.id

        # 设置创建时间
        validated_data['create_time'] = timezone.now()

        # 创建角色
        return super().create(validated_data)


class MenuOptionSerializer(serializers.ModelSerializer):
    """菜单选项序列化器"""
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='name')
    children = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = ['value', 'label', 'children']

    @staticmethod
    def get_children(obj):
        """递归获取子菜单"""
        children = Menu.objects.filter(parent_id=obj.id, visible=1).order_by('sort', 'id')
        if children.exists():
            return MenuOptionSerializer(children, many=True).data
        return []
